package com.all.play.mypsm.App;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.mypsm.R;
import com.all.play.mypsm.home.bean.GoodsInfo;
import com.all.play.mypsm.shoppingtcart.utils.CartSorage;
import com.all.play.mypsm.utils.Constants;
import com.bumptech.glide.Glide;

import static android.view.KeyEvent.KEYCODE_BACK;

/**
 * Created by 聪明一只哈 on 2018/3/31.
 */

public class GoodsInfoActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton ibGoodInfoBack;
    private ImageButton ibGoodInfoMore;
    private ImageView ivGoodInfoImage;
    private TextView tvGoodInfoName;
    private TextView tvGoodInfoDesc;
    private TextView tvGoodInfoPrice;
    private TextView tvGoodInfoStore;
    private TextView tvGoodInfoStyle;
    private WebView wbGoodInfoMore;
    private LinearLayout llGoodsRoot;
    private TextView tvGoodInfoCallcenter;
    private TextView tvGoodInfoCollection;
    private TextView tvGoodInfoCart;
    private Button btnGoodInfoAddcart;

    private TextView tv_more_share, tv_more_search, tv_more_home;
    private Button btn_more;
    private GoodsInfo goodsinfo;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodsinfo);
        //初始化view
        findViews();

        //获取传递过来的数据
        goodsinfo = (GoodsInfo) getIntent().getExtras().get("goodsinfo");
        //设置获取的数据
        if (goodsinfo != null) {
            setData(goodsinfo);
        }
    }

    /**
     * 设置传递过来的数据
     *
     * @author 聪明一只哈
     * @time 2018/4/2 8:49
     */
    private void setData(GoodsInfo goodsinfo) {
        //设置图片
        Glide.with(this).load(Constants.BASE_URl_IMAGE + goodsinfo.getFigure()).into(ivGoodInfoImage);
        //设置name
        tvGoodInfoName.setText(goodsinfo.getName());

        //设置价格
        tvGoodInfoPrice.setText("￥"+goodsinfo.getCover_price()+"元");

        //设置webview
        setwebData(goodsinfo.getProduct_id());


    }

    /**
     *给第二个ScrollView设置数据
     *@author 聪明一只哈
     *@time 2018/4/2 9:04
     */
    private void setwebData(String preduct_id) {
       if(preduct_id!=null){

           //webView.loadUrl("http://www.sina.com.cn");
           //webView.loadUrl("http://www.4399.com/flash/18012.htm");
           //webView.loadUrl("file:///android_asset/1.html");
           wbGoodInfoMore.loadUrl("http://www.4399.com");
           //webView.loadUrl("https://www.sina.com.cn");
           //www.admin.weitengchina.com   admin123



           //声明WebSettings子类
           WebSettings webSettings = wbGoodInfoMore.getSettings();
           //设置自适应屏幕，两者合用
           wbGoodInfoMore.setInitialScale(1);//这个很重要
           webSettings.setUseWideViewPort(true);
           webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
           webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
           //缩放操作
           webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
           webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
           webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
           webSettings.setAllowContentAccess(true);
           //支持通过JS打开新窗口
           webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
           webSettings.setJavaScriptEnabled(true);

           //优先使用缓存:
           webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
           //缓存模式如下：
           //LOAD_CACHE_ONLY: 不使用网络，只读取本地缓存数据
           //LOAD_DEFAULT: （默认）根据cache-control决定是否从网络上取数据。
           //LOAD_NO_CACHE: 不使用缓存，只从网络获取数据.
           //LOAD_CACHE_ELSE_NETWORK，只要本地有，无论是否过期，或者no-cache，都使用缓存中的数据。



          wbGoodInfoMore.setWebViewClient(new WebViewClient() {

              /*
              @Override
               public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;

                   //这种方法解决重定向问题   按返回键,不起作用,死循环
                   WebView.HitTestResult hitTestResult = view.getHitTestResult();
                   //hitTestResult==null解决重定向问题
                   if (!TextUtils.isEmpty(url) && hitTestResult == null) {
                       view.loadUrl(url);
                       return true;
                   }
                   //WebBackForwardList mWebBackForwardList = webview.copyBackForwardList();//获取栈内存在的URL
                   //for (int i = 0, p = mWebBackForwardList.getSize(); i < p; i++) {
                   //  Log.e("====", "" + mWebBackForwardList.getItemAtIndex(i).getUrl());
                   //}
                   return false;
               }*/

               @Override
               public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                       view.loadUrl(request.getUrl().toString());
                   }

                   return true;
               }
           });
          /* wbGoodInfoMore.loadUrl("http://www.4399.com/flash/18012.htm");
           //设置支持JavaScript
           WebSettings webSettings = wbGoodInfoMore.getSettings();
           webSettings.setUseWideViewPort(true);//支持双击页面变大变小
           webSettings.setJavaScriptEnabled(true);//设置支持JavaScript
           //优先使用缓存
           webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
           wbGoodInfoMore.setWebViewClient(new WebViewClient(){

               @Override
               public boolean shouldOverrideUrlLoading(WebView view, String url) {
                   //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                   view.loadUrl(url);
                   return true;
               }
//                @Override
//                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                        view.loadUrl(request.getUrl().toString());
//                    }
//                    return true;
//                }
           });*/

       }
    }


    /**
     * Find the Views in the layout<br />
     * <br />
     * Auto-created on 2018-03-31 17:38:16 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    private void findViews() {
        tv_more_share = (TextView) findViewById(R.id.tv_more_share);
        tv_more_search = (TextView) findViewById(R.id.tv_more_search);
        tv_more_home = (TextView) findViewById(R.id.tv_more_home);
        btn_more = (Button) findViewById(R.id.btn_more);


        ibGoodInfoBack = (ImageButton) findViewById(R.id.ib_good_info_back);
        ibGoodInfoMore = (ImageButton) findViewById(R.id.ib_good_info_more);
        ivGoodInfoImage = (ImageView) findViewById(R.id.iv_good_info_image);
        tvGoodInfoName = (TextView) findViewById(R.id.tv_good_info_name);
        tvGoodInfoDesc = (TextView) findViewById(R.id.tv_good_info_desc);
        tvGoodInfoPrice = (TextView) findViewById(R.id.tv_good_info_price);
        tvGoodInfoStore = (TextView) findViewById(R.id.tv_good_info_store);
        tvGoodInfoStyle = (TextView) findViewById(R.id.tv_good_info_style);
        wbGoodInfoMore = (WebView) findViewById(R.id.wb_good_info_more);
        llGoodsRoot = (LinearLayout) findViewById(R.id.ll_goods_root);
        tvGoodInfoCallcenter = (TextView) findViewById(R.id.tv_good_info_callcenter);
        tvGoodInfoCollection = (TextView) findViewById(R.id.tv_good_info_collection);
        tvGoodInfoCart = (TextView) findViewById(R.id.tv_good_info_cart);
        btnGoodInfoAddcart = (Button) findViewById(R.id.btn_good_info_addcart);

        ibGoodInfoBack.setOnClickListener(this);
        ibGoodInfoMore.setOnClickListener(this);
        btnGoodInfoAddcart.setOnClickListener(this);

        //客服
        tvGoodInfoCallcenter.setOnClickListener(this);
        //收藏
        tvGoodInfoCollection.setOnClickListener(this);
        //购物车
        tvGoodInfoCart.setOnClickListener(this);

        //更多-------------
        tv_more_share.setOnClickListener(this);
        tv_more_search.setOnClickListener(this);
        tv_more_home.setOnClickListener(this);
        btn_more.setOnClickListener(this);

    }


    /**
     * Handle button click events<br />
     * <br />
     * Auto-created on 2018-03-31 17:38:16 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    @Override
    public void onClick(View v) {
        if (v == ibGoodInfoBack) {
            // Handle clicks for ibGoodInfoBack
            finish();
        } else if (v == ibGoodInfoMore) {
            // Handle clicks for ibGoodInfoMore
            Toast.makeText(GoodsInfoActivity.this, "更多", Toast.LENGTH_SHORT).show();
        } else if (v == btnGoodInfoAddcart) {
            // Handle clicks for btnGoodInfoAddcart

            CartSorage.getInstance().addData(goodsinfo);

            Toast.makeText(GoodsInfoActivity.this, "添加到购物车", Toast.LENGTH_SHORT).show();
        } else if (v == tvGoodInfoCallcenter) {
            // Handle clicks for btnGoodInfoAddcart
            Toast.makeText(GoodsInfoActivity.this, "客服", Toast.LENGTH_SHORT).show();
        } else if (v == tvGoodInfoCollection) {
            // Handle clicks for btnGoodInfoAddcart
            Toast.makeText(GoodsInfoActivity.this, "收藏", Toast.LENGTH_SHORT).show();
        } else if (v == tvGoodInfoCart) {
            // Handle clicks for btnGoodInfoAddcart
            Toast.makeText(GoodsInfoActivity.this, "购物车", Toast.LENGTH_SHORT).show();
        } else if (v == tv_more_share) {
            // Handle clicks for btnGoodInfoAddcart
            Toast.makeText(GoodsInfoActivity.this, "tv_more_share", Toast.LENGTH_SHORT).show();
        } else if (v == tv_more_search) {
            // Handle clicks for btnGoodInfoAddcart
            Toast.makeText(GoodsInfoActivity.this, "tv_more_search", Toast.LENGTH_SHORT).show();
        } else if (v == tv_more_home) {
            // Handle clicks for btnGoodInfoAddcart
            Toast.makeText(GoodsInfoActivity.this, "tv_more_home", Toast.LENGTH_SHORT).show();
        } else if (v == btn_more) {
            // Handle clicks for btnGoodInfoAddcart
            Toast.makeText(GoodsInfoActivity.this, "btn_more", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KEYCODE_BACK) && wbGoodInfoMore.canGoBack()) {
            wbGoodInfoMore.goBack();
            return true;
        }


        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        wbGoodInfoMore.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        wbGoodInfoMore.onPause();

    }
}
